package cn.itcast.utils;


import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

public class MailUtil {

   /**
    *
    * @param to 收件人
    * @param subject  主题
    * @param content 内容
    * @throws Exception
    */
   //实现邮件发送的方法
   public static void sendMsg(String to ,String subject ,String content) throws Exception{
      Properties props = new Properties();
      props.setProperty("mail.smtp.host", "smtp.sina.com");  //设置主机地址   smtp.qq.com    smtp.sina.com

      props.setProperty("mail.smtp.auth", "true");//授权认证 代码客户端访问 必须设置为true  需要手机验证

      //2.产生一个用于邮件发送的Session对象
      Session session = Session.getInstance(props);

      //3.产生一个邮件的消息对象
      MimeMessage message = new MimeMessage(session);

      //4.设置消息的发送者
      Address fromAddr = new InternetAddress("gzc_lxf@sina.com");
      message.setFrom(fromAddr);

      //5.设置消息的接收者
      Address toAddr = new InternetAddress(to);
      //TO 直接发送  CC抄送    BCC密送
      message.setRecipient(MimeMessage.RecipientType.TO, toAddr);

      //6.设置主题
      message.setSubject(subject);
      //7.设置正文
      message.setText(content);

      //8.准备发送，得到火箭
      Transport transport = session.getTransport("smtp");
      //9.设置火箭的发射目标
      transport.connect("smtp.sina.com", "gzc_lxf@sina.com", "6249824d598f0b1c"); //密码 授权密码!=登陆密码
      //10.发送
      transport.sendMessage(message, message.getAllRecipients());

      //11.关闭
      transport.close();
   }

   public static void main(String[] args) throws Exception {
      MailUtil.sendMsg("gzc_njsp@126.com" , "我们去玩把", "你好");
   }

}